package com.xidian.zhiancloud.service.myyb;

import com.xidian.zhiancloud.util.tools.WeChatUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @program: baseframe
 * @description: 客服消息自动回复
 * @author: miyaxin
 * @create: 2020-07-16 11:01
 **/
@RestController
@RequestMapping("api/customerService")
public class XcxCustomerServiceApi {

    private static final Logger logger = LoggerFactory.
            getLogger(XcxCustomerServiceApi.class);

    // Token(令牌) xinchejie0xiaoxizidonghuifu
    // EncodingAESKey
    // (消息加密密钥) QnNZGJgAYGDgC0LMrqgi4P1ZRi3heLzYWy6WGj7V8lE

    /**
     * 处理微信服务器发来的get请求，进行签名的验证
     *
     * signature 微信端发来的签名
     * timestamp 微信端发来的时间戳
     * nonce   微信端发来的随机字符串
     * echostr  微信端发来的验证字符串uploadFileToWeChat
     */
    @RequestMapping(value = "/message", method = {RequestMethod.GET, RequestMethod.POST})
    public String message(@RequestParam(required = false) String signature,
                          @RequestParam(required = false) String timestamp,
                          @RequestParam(required = false) String nonce,
                          @RequestParam(required = false) String echostr,
                          @RequestBody(required = false) Map<String,Object> params) {
        logger.info("消息开始接收 ==------------》 ");
        logger.info("数据包"+params);
        //处理消息数据
        if(params != null){
            handleSend(params);
        }
        logger.info("signature == " + signature);
        logger.info("timestamp == " + timestamp);
        logger.info("nonce == " + nonce);
        logger.info("echostr == " + echostr);
        logger.info("params == " + params);
        // 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
        if (signature != null && WeChatUtil.checkSignature(signature, timestamp, nonce)) {
            return echostr;
        }else{
            return null;
        }
    }

    /**
     * 处理客服发送的客服消息
     * @param params
     */
    private void handleSend(Map<String,Object> params){
        //获取路径参数
        if(params.containsKey("PagePath") && params.get("PagePath") != null){
            String pagePath = params.get("PagePath").toString();
            if(StringUtils.isNotEmpty(pagePath)){
                //获取销售顾问id
                String staffId = pagePath.substring(pagePath.lastIndexOf('/') + 1).trim();
                //获取发送者openId
                String openId = params.get("FromUserName").toString();
                replySend(staffId,openId);
            }
        }
    }

    /**
     * 小程序自动回复消息接口
     * @return
     */
    public String replySend(String staffId,String openId){
//        if(StringUtils.isNotEmpty(staffId)){
//            //查询销售顾问二维码图片
//            Map<String,Object> staffMap = memberBaseService.
//                    queryStaff(Integer.parseInt(staffId));
//            if(staffMap.get("wxQrCode") != null){
//                //获取微信平台上传文件的 媒体文件id
//                String img = "http://qiniu.zhonghaiev.com/"+staffMap.get("wxQrCode")+"";
//                String media_id = weChatService.uploadFileToWeChat("image",staffMap,img);
//                logger.info("media_id----->"+media_id);
//                if(StringUtils.isNotEmpty(media_id)){
//                    //获取回复消息url
//                    String url = weChatService.replySend(staffMap);
//                    logger.info("发送url----->"+url);
//                    Map<String,String> map = new HashMap<String,String>();
//                    map.put("openId",openId);
//                    map.put("media_id",media_id);
//                    map.put("url",url);
//                    String json = WeChatUtil.sendArticleMsg(map);
//                    logger.info("返回结果----->"+json);
//                    return json;
//                }
//            }
//            return "顾问二维码图片为空";
//        }
        return "顾问id不能为空";
    }


}
